#ifndef __REGWRAP_H
#define __REGWRAP_H

#include <string>
#include <vector>
#include <windows.h>

typedef std::string tstring;

#pragma comment(lib,"shlwapi.lib")

//==========================================================================>>
// A primitive helper class to assist with registry reading/writing
//==========================================================================>>

class RegWrap
{
public:
    RegWrap( HKEY hRootKey, const tstring& sSubKey, bool bCreate = false ) : hkSession(0) { Init( hRootKey, sSubKey, bCreate ); }
    RegWrap( const tstring& sKey, bool bCreate = false );
    virtual ~RegWrap();

    bool operator!() const { return hkSession == 0; }

    tstring ReadString( const tstring& sName, const char *szDefaultValue = 0 ) const;
    unsigned long ReadDword( const tstring& sName, unsigned long dwDefaultValue = 0 ) const;

    HRESULT WriteString( const tstring& sName, const tstring& sValue ) const;
    HRESULT WriteDword( const tstring& sName, unsigned long dwValue ) const;

    HRESULT DeleteValue( const tstring& sName ) const;
    HRESULT DeleteKey( const tstring& sName ) const;

    HRESULT EnumKeys( std::vector<tstring>& v ) const;
    HRESULT EnumValues( std::vector<tstring>& v ) const;

    long GetLastError() { return nInitResult; }
private:
    void Init( HKEY hRootKey, const tstring& sSubKey, bool bCreate );
    long ReadToBuf( const tstring& sName, unsigned long &dwValueType, unsigned char *buf, unsigned long &dwBufSize ) const;
    HKEY hkSession;
    long nInitResult; // The result of the ::RegCreateKeyEx/::RegOpenKeyEx operation

private:
    RegWrap( const RegWrap& );
    const RegWrap& operator=( const RegWrap& );
};

#endif // __REGWRAP_H
